/**
 * reflection bitmap
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2008. 08. 25
*/
package com.coo.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	/**
	 * Reflection.
	 * 
	 * @example
 	 * <listing> 
 	 * package {
     *    import flash.display.Sprite;
     *    import com.cmiscm.utils.display.Reflection;
     *    import flash.display.Bitmap;
     *    import flash.display.BitmapData;
     *    import flash.events.Event;
     *    
     *    public class TestError extends Sprite
     *    {
     *        public function TestError()
     *        {
     *             super();
     *             this.addEventListener(Event.ADDED_TO_STAGE, onAdd);
     *         }
     * 
     *         private function onAdd(evt:Event):void
     *         {
     *             evt.currentTarget.removeEventListener(evt.type, arguments.callee);
     * 
     *             var bitData:Bitmap = new Bitmap(new imgTest(0, 0));
     *             var reflectData:BitmapData = Reflection.setReflection(bitData, 300, 170, 50, 2, true, 0x00000000, 0.5);
     *             var bit:Bitmap = new Bitmap(reflectData);
     *             this.addChild(bit);
     *        }
     *    }
     * }
     * </listing> 
     * <p>
 	 * Result:</p>
 	 * <img src = "Reflection0.jpg" />
     */	
	public class Reflection extends Bitmap
	{
		/**
		 * Make Reflection
		 * <p>
		 * DisplayObject을 받아서 reflection 적용 후 BitmapData 반환
		 * @param $mc Bitmap
		 * @param $mcW Bitmap width
		 * @param $mcH Bitmap height
		 * @param $graH Gradation height
		 * @param $graY Gradation y position
		 * @param $transparent Bitmap transparent
		 * @param $color Reflection bitmapdata bg color
		 * @param $alpha Gradation first alpha
		 * @return 
		 * 
		 */		
		public static function setReflection($mc:DisplayObject, $mcW:int, $mcH:int, $graH:int, $graY:int=0, $transparent:Boolean=false, $color:int=0x00000000, $alpha:Number=0.5):BitmapData
		{
			// reflection image
			var refBitmapData:BitmapData = new BitmapData($mcW, $graH, $transparent, $color);
			var refMat:Matrix = new Matrix();
			refMat.translate(0, $mcH);
			refMat.d = -1;			
			refBitmapData.draw($mc, refMat);
			var bitRef:Bitmap = new Bitmap(refBitmapData);
			bitRef.y = $mcH + $graY;
			
			// reflection gradation
			var fillType:String = GradientType.LINEAR;
			var colors:Array = [0x000000, 0x000000];
			var alphas:Array = [$alpha, 0];
			var ratios:Array = [0, 255];
			var graMat:Matrix = new Matrix();
			graMat.createGradientBox($mcW, $graH, (90/180)*Math.PI, 0, 0);
			var mcGra:Sprite = new Sprite();
			mcGra.graphics.beginGradientFill(fillType, colors, alphas, ratios, graMat);
			mcGra.graphics.drawRect(0, 0, $mcW, $graH);
			mcGra.graphics.endFill();
			mcGra.y = $mcH + $graY;
			
			// mc
			var mcBitmapData:BitmapData = new BitmapData($mcW, $mcH, $transparent, $color);
			mcBitmapData.draw($mc);
			var bitMc:Bitmap = new Bitmap(mcBitmapData);
			
			// mask
			mcGra.cacheAsBitmap = bitRef.cacheAsBitmap = true;			
			bitRef.mask = mcGra;
			
			// addChild
			var mcCon:Sprite = new Sprite();
			mcCon.addChild(bitMc);
			mcCon.addChild(bitRef);			
			mcCon.addChild(mcGra);	
			
			// bitmap
			var conBitmapData:BitmapData = new BitmapData($mcW, $mcH + $graH + $graY, $transparent, $color);		
			conBitmapData.draw(mcCon);
			
			// dispose
			refBitmapData.dispose();
			bitRef = null;
			mcBitmapData.dispose();
			bitMc = null;
			mcGra.graphics.clear();
			mcGra = null;
			
			return conBitmapData;
		}
	}
}